<template>
	<view class="follow">
		<view class="header">
			<view class="headnavi">
				<image class="arrow" src="../../static/home/main/details/arrow.png" @click="goBack()"></image>
				<view class="navi-title">关注和粉丝</view>
			</view>
			<scroll-view class="top-menu-view" scroll-x="true">
				<view class="menu-topic-view" v-for="item in tabs" :id="'tabNum'+item.id" :key="(item.id - 1)"
					@click="swichMenu(item.id - 1)">
					<view :class="currentTab==(item.id - 1) ? 'menu-topic-act' : 'menu-topic'">
						<text class="menu-topic-text">{{item.name}}<!-- {{item.count}} --></text>
						<view class="menu-topic-bottom">
							<view class="menu-topic-bottom-color"></view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>

		<view class="body">
			<view class="following-container" v-if="currentTab===0" v-for="(user,index) in following" :key="index">
				<image :src="user.avatarSrc" class="avatar"></image>
				<text class="username">{{user.username}}</text>
			</view>
			<view class="follower-container" v-if="currentTab===1" v-for="(user,index) in follower" :key="index">
				<image :src="user.avatarSrc" class="avatar"></image>
				<text class="username">{{user.username}}</text>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabs: [{
						id: 1,
						name: '关注',
						/* count: 0, */
					},
					{
						id: 2,
						name: '粉丝',
						/* count: 0, */
					},
				],

				following: [{
					id: '',
					username: '',
					avatarSrc: '',
				}, ],

				follower: [{
					id: '',
					username: '',
					avatarSrc: '',
				}, ],

				currentTab: 0,

				userInfo: {
					id: '',
				},

			};

		},
		onShow() {
			const userInfo = uni.getStorageSync('userInfo');
			this.userInfo.id = userInfo.data.id;
			this.getFans();
			this.getFollowers();
		},
		mounted() {



		},
		onLoad(options) {
			// 从url中获取index参数  
			if (options.index) {
				this.currentTab = parseInt(options.index); // 转换为整数类型  
			}
			// 其他的页面加载逻辑  
		},
		methods: {
			goBack() {
				uni.switchTab({
					url: '/pages/my/my'
				})
			},
			swichMenu(id) {
				this.currentTab = id
				console.log(11, id)
				/* this.currentTab = 'tabNum' + id */
			},
			getFans() {
				const url = 'http://localhost:8080/user/' + this.userInfo.id + '/fans';
				uni.request({
					url: url,
					method: 'GET',
					success: (res) => {
						this.follower = [];
						for (var i = 0; i < res.data.data.length; i++) {
							const newFollower = {
								id: 0,
								userName: '',
								avatarId: 0,
								avatarSrc: '',
							};
							newFollower.id = res.data.data[i].id;
							newFollower.username = res.data.data[i].userName;
							newFollower.avatarId = res.data.data[i].avatar;
							console.log(newFollower.avatarId);
							if (!newFollower.avatarId) {
								newFollower.avatarSrc = '/static/chishenme/morentouxiang.jpg';
							} else {
								uni.request({ //获取图片
									url: 'http://localhost:8080/image/getImage?id=' + newFollower
										.avatarId,
									method: 'GET',
									success: res1 => {

										const arrayBuffer = uni.base64ToArrayBuffer(res1.data.data
											.data);
										// 将ArrayBuffer转换为Blob对象
										const blob = new Blob([arrayBuffer], {
											type: 'image/png'
										});
										// 创建Blob URL
										const imgUrl = URL.createObjectURL(blob);
										newFollower.avatarSrc = imgUrl;
										// this.button1img = imgUrl;
									},
									fail: (err) => {
										console.error(err);
										// 在这里处理请求失败的逻辑
									}
								});
							}
							this.follower.push(newFollower);
						}
						/* console.log(res);
						console.log(this.follower); */
					},
					fail: (err) => {
						console.error(err);
						// 在这里处理请求失败的逻辑
					}
				});
			},
			getFollowers() {
				const url = 'http://localhost:8080/user/' + this.userInfo.id + '/followers';
				uni.request({
					url: url,
					method: 'GET',
					success: (res) => {
						this.following = [];
						for (var i = 0; i < res.data.data.length; i++) {
							const newFollowing = {
								id: 0,
								userName: '',
								avatarId: 0,
								avatarSrc: '',
							};
							newFollowing.id = res.data.data[i].id;
							newFollowing.username = res.data.data[i].userName;
							newFollowing.avatarId = res.data.data[i].avatar;
							console.log(newFollowing.avatarId);
							if (!newFollowing.avatarId) {
								newFollowing.avatarSrc = '/static/chishenme/morentouxiang.jpg';
							} else {
								uni.request({ //获取图片
									url: 'http://localhost:8080/image/getImage?id=' + newFollower
										.avatarId,
									method: 'GET',
									success: res1 => {
					
										const arrayBuffer = uni.base64ToArrayBuffer(res1.data.data
											.data);
										// 将ArrayBuffer转换为Blob对象
										const blob = new Blob([arrayBuffer], {
											type: 'image/png'
										});
										// 创建Blob URL
										const imgUrl = URL.createObjectURL(blob);
										newFollowing.avatarSrc = imgUrl;
										// this.button1img = imgUrl;
									},
									fail: (err) => {
										console.error(err);
										// 在这里处理请求失败的逻辑
									}
								});
							}
							this.following.push(newFollowing);
						}
						/* console.log(res);
						console.log(this.follower); */
					},
					fail: (err) => {
						console.error(err);
						// 在这里处理请求失败的逻辑
					}
				});
			},
		},
	}
</script>

<style lang="scss">
	.follow {
		display: flex;
		flex-direction: column;

		.header {
			display: flex;
			flex-direction: column;
			position: fixed;
			z-index: 2;

			.headnavi {
				/* height: 10%; */
				width: 100vw;
				background-color: white;
				/* position: sticky; */
				display: flex;

				box-shadow: 0rpx 10rpx 20rpx 0rpx #e5e5e5;
				padding-bottom: 1vh;

				.arrow {
					/* transform: rotate(180deg); */
					width: 6vw;
					height: 3vh;
					margin-top: 5vh;
					margin-left: 5vw;
				}

				.navi-title {
					margin-top: 5vh;
					margin-left: 28vw;
					font-size: 40rpx;
					color: #FF8D1A;

				}
			}

			.top-menu-view {

				display: flex;
				/* position: sticky; */
				top: 0;
				z-index: 3;
				white-space: nowrap;
				width: 100%;
				background-color: #FFFFFF;
				height: 6vh;
				line-height: 100rpx;
				border-bottom: 1rpx solid #d8dbe6;

				/* border-radius: 35rpx 35rpx 0rpx 0rpx; */

				.menu-topic-view {

					display: inline-block;
					white-space: nowrap;
					height: 6vh;
					position: relative;
					margin-left: 35rpx;

					.menu-topic-text {
						font-size: 36rpx;
						color: #303133;
						padding: 25rpx 35rpx;
					}

					// .menu-topic-act {
					// 	margin-left: 30upx;
					// 	margin-right: 10upx;
					// 	position: relative;
					// 	height: 100%;
					// 	display: flex;
					// 	align-items: center;
					// 	justify-content: center;
					// }

					.menu-topic-bottom {
						position: absolute;
						bottom: 0;
						width: 100%;

						.menu-topic-bottom-color {
							width: 50rpx;
							height: 4rpx;
						}
					}

					.menu-topic-act .menu-topic-bottom {

						display: flex;
						justify-content: center;
					}

					.menu-topic-act .menu-topic-bottom-color {
						background: #FF8D1A;
					}

					.menu-topic-act .menu-topic-text {
						color: #FF8D1A;
					}
				}
			}
		}


		.body {
			width: 100vw;
			display: flex;
			flex-direction: column;
			margin-top: 15vh;


			.following-container {
				padding-left: 35rpx;
				padding-left: 35rpx;
				padding-top: 17rpx;
				padding-bottom: 17rpx;
				display: flex;
				border-bottom: 1rpx solid #d8dbe6;

				.avatar {
					border-radius: 20vw;
					width: 20vw;
					height: 20vw;
				}

				.username {
					align-self: center;
					padding-left: 35rpx;
					font-size: 35rpx;
				}
			}

			.follower-container {
				padding-left: 35rpx;
				padding-left: 35rpx;
				padding-top: 17rpx;
				padding-bottom: 17rpx;
				display: flex;
				border-bottom: 1rpx solid #d8dbe6;

				.avatar {
					border-radius: 20vw;
					width: 20vw;
					height: 20vw;
				}

				.username {
					align-self: center;
					padding-left: 35rpx;
					font-size: 35rpx;
				}
			}
		}
	}
</style>